﻿using Architecture.API.Application.Command.RoleCommand;
using Architecture.Domain;
using Architecture.Domain.Dto.RoleDto;
using Architecture.Domain.Service.RoleServices;
using Architecture.Repository.Impl;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--角色
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleManagementController : ControllerBase
    {
        /// <summary>
        /// 角色信息服务
        /// </summary>
        protected readonly IRoleService roleService;

        /// <summary>
        /// 日志
        /// </summary>
        protected readonly ILogger<RoleManagementController> logger;

        /// <summary>
        ///  中介者
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleService">角色信息服务</param>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public RoleManagementController(IRoleService roleService, ILogger<RoleManagementController> logger, IMediator mediator)
        {
            this.roleService = roleService;
            this.logger = logger;
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="role">角色实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddRole(Role role)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var res = await roleService.GetRole(roleName:role.RoleName);
                if (res.Count>0)
                {
                    result.Code = -2;
                }
                else
                {
                    result.Data = await roleService.AddRole(role);
                }
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取角色信息列表
        /// </summary>
        /// <returns>返回角色信息列表</returns>
        [HttpGet]
        public async Task<ApiResult<PagingRoleDto<Role>>> GelRoleList(RoleSerchCommand command)
        {
            ApiResult<PagingRoleDto<Role>> result = new ApiResult<PagingRoleDto<Role>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="roleId">角色主键</param>
        /// <returns>返回受影响的行数</returns>
        [HttpGet]
        public async Task<ApiResult<int>> DeletedRole(int roleId)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await roleService.DeletedRole(roleId);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询并反填角色--用户信息用
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>反填角色信息</returns>
        [HttpGet]
        public async Task<ApiResult<object>> GetRoleByRoleId(int roleId)
        {
            ApiResult<object> result = new ApiResult<object>();
            try
            {
                result.Data = await roleService.GetModel(roleId);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="role">角色信息</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateRole(Role role)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //角色名称重名判断
                //var res = await roleService.GetRole(roleName: role.RoleName);
                //if (res.Count > 0)
                //{
                //    result.Code = -2;
                //}
                //else
                //{
                //    result.Data = await roleService.UpdateRole(role);
                //}

                result.Data = await roleService.UpdateRole(role);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填角色信息
        /// </summary>
        /// <param name="roleId">用户ID</param>
        /// <returns>反填角色信息</returns>
        [HttpGet]
        public async Task<ApiResult<Role>> GetModelById(int roleId)
        {
            ApiResult<Role> result = new ApiResult<Role>();
            try
            {
                result.Data = await roleService.GetModelById(roleId);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加角色 --MediatR中介者模式
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(RoleCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询角色全部信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回角色全部信息</returns>
        [HttpPost]
        public async Task<ApiResult<PagingRoleDto<Role>>> GetAll(RoleSerchCommand command)
        {
            ApiResult<PagingRoleDto<Role>> result = new ApiResult<PagingRoleDto<Role>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> Update(RoleUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> Remove(RoleRemoveCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data= await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询并反填角色--用户信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>反填角色--用户信息</returns>
        [HttpPost]
        public async Task<ApiResult<object>> GetRoleModel(RoleReverseFillingCommand command)
        {
            ApiResult<object> result = new ApiResult<object>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填角色信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>反填角色信息</returns>
        [HttpPost]
        public async Task<ApiResult<Role>> SearchBackRole(RoleReverseCommand command)
        {
            ApiResult<Role> result = new ApiResult<Role>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询角色部分信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回部分角色信息</returns>
        [HttpPost]
        public async Task<ApiResult<object>> SearchRoleInfo(RoleSearchInfoCommand command)
        {
            ApiResult<object> result = new ApiResult<object>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
